<?php

/**
 * 
 * @author Administrator
 * 百度编辑器
 */
class UeditorController extends Controller {
	private $config = array();
	public function __construct($id,$module=null){
		parent::__construct();
		
		date_default_timezone_set("Asia/chongqing");
		error_reporting(E_ERROR);
		header("Content-Type: text/html; charset=utf-8");
	}
	
	public function index(){
		$this->config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("../public/static/plugin/ueditor/config.json")), true);
		$action = $_GET['action'];
		switch ($action) {
			case 'config':
				$result =  json_encode($this->config);
				break;
		
				/* 上传图片 */
			case 'uploadimage':
				/* 上传涂鸦 */
			case 'uploadscrawl':
				/* 上传视频 */
			case 'uploadvideo':
				/* 上传文件 */
			case 'uploadfile':				
				$result = UeditorService::action_upload($this->config,$action);
				break;
		
				/* 列出图片 */
			case 'listimage':
				$result = UeditorService::action_list($this->config,$action);
				break;
				/* 列出文件 */
			case 'listfile':
				$result = UeditorService::action_list($this->config,$action);
				break;
		
				/* 抓取远程文件 */
			case 'catchimage':
				$result = UeditorService::action_crawler($this->config);
				break;
		
			default:
				$result = json_encode(array(
				'state'=> '请求地址出错'
						));
						break;
		}
		
		/* 输出结果 */
		if (isset($_GET["callback"])) {
			if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
				echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
				exit;
			} else {
				echo json_encode(array(
						'state'=> 'callback参数不合法'
				));
				exit;
			}
		} else {
			echo $result;
			exit;
		}
	}
	
	
	
	

}